package libContext

import (
	"database/sql"
	"gitee.com/libwiki/go-zero-lib/package/orm"
	"gitee.com/libwiki/go-zero-lib/package/setting"
	"gorm.io/gorm"
	"sync"
)

type LibContext interface {
	InitContext(c setting.Config)
}

type Context struct {
	Orm   *orm.Database
	Db    *gorm.DB
	SqlDb *sql.DB
}

var once sync.Once
var instance *Context

func NewContext(Orm *orm.Database) *Context {
	return &Context{Orm: Orm, Db: Orm.GormDb}
}

func NewSingleContext(Orm *orm.Database) *Context {
	if instance == nil {
		once.Do(func() {
			instance = &Context{Orm: Orm, Db: Orm.GormDb}
		})
	}
	return instance
}

func (this *Context) InitContext(c setting.Config) {
	Orm := orm.NewDatabase(c.Database)
	this.Orm = Orm
	this.Db = Orm.GormDb
	this.SqlDb = Orm.SqlDb
}
